<!-- @format -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.min.js"></script>
  </head>
  <body>
    <input id="fileInput" type="file" multiple />
    <script>
      const fileInput = document.querySelector("#fileInput");

      //   async function formData() {
      //     const data = new FormData();
      //     data.set("name", "光");
      //     data.set("age", 20);
      //     data.set("fileUpload", fileInput.files[0]);

      //     const res = await axios.post("http://localhost:3000/fileUpload", data);
      //     console.log(res);
      //   }
      async function formData() {
        const data = new FormData();
        data.set("name", "光");
        data.set("age", 20);
        // 这里fileInput.files 是一个伪数组，要转成数组才能用 forEach 方法
        // [...fileInput.files].forEach((item) => {
        //   data.append("fileUploads", item);
        // });
        data.append("aaa", fileInput.files[0]);
        data.append("aaa", fileInput.files[1]);
        data.append("bbb", fileInput.files[2]);
        data.append("bbb", fileInput.files[3]);

        const res = await axios.post("http://localhost:3000/fileUploads", data);
        console.log(res);
      }

      fileInput.onchange = formData;
    </script>
  </body>
</html>
